package com.cornergj.back.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cornergj.model.Admin;
import com.cornergj.model.Entity;
import com.cornergj.model.Entity.AdminCriteria;
import com.cornergj.model.Entity.FrontImgCriteria;
import com.cornergj.model.Entity.OrderServiceCriteria;
import com.cornergj.model.Entity.Pagination;
import com.cornergj.model.Entity.ServiceContentCriteria;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.FrontImg;
import com.cornergj.model.Nanny;
import com.cornergj.model.OrderService;
import com.cornergj.model.ServiceContent;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.AdminService;
import com.cornergj.service.FrontImgService;
import com.cornergj.service.NannyService;
import com.cornergj.service.OrderServiceService;
import com.cornergj.service.ServiceContentService;
import com.cornergj.uitl.FileUtils;


/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年1月31日
 */
@Controller
@RequestMapping("/order")
public class OrderMagementController {
	
	@Autowired
	private ServiceContentService serviceContentService;
	@Autowired
	private OrderServiceService orderServiceService;
	@Autowired
	private NannyService nannyService;
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, OrderService bean, Pagination pagination) {
		//查询条件封装
		OrderServiceCriteria criteria = new OrderServiceCriteria();
		if(bean != null && bean.getUserId()!= null && !"".equals(bean.getUserId())){
			criteria.setUserId(Value.eq(bean.getUserId()));
		}
		//分页查询
		Pagination list = orderServiceService.selectPage(criteria, pagination);
		List<ServiceContent> servicelist=serviceContentService.selectAll();
		request.setAttribute("pagination", list);
		request.setAttribute("servicelist", servicelist);
		request.setAttribute("bean", bean);
		return "order/list";
	}
	
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/alllist", method = RequestMethod.GET)
	public String alllist(HttpServletRequest request, OrderService bean, Pagination pagination) {
		//查询条件封装
		OrderServiceCriteria criteria = new OrderServiceCriteria();
		if(bean != null && bean.getOrderCode()!= null && !"".equals(bean.getOrderCode())){
			criteria.setOrderCode(Value.like("%"+bean.getOrderCode()+"%"));
		}
		//分页查询
		Pagination list = orderServiceService.selectPage(criteria, pagination);
		List<ServiceContent> servicelist=serviceContentService.selectAll();
		request.setAttribute("pagination", list);
		request.setAttribute("servicelist", servicelist);
		request.setAttribute("bean", bean);
		return "order/alllist";
	}
	
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/unarrorderlist", method = RequestMethod.GET)
	public String unarrlist(HttpServletRequest request, OrderService bean, Pagination pagination) {
		//查询条件封装
		OrderServiceCriteria criteria = new OrderServiceCriteria();
		if(bean != null && bean.getArrange()!= null && !"".equals(bean.getArrange())){
			criteria.setArrange(Value.eq(bean.getArrange()));
		}
		//分页查询
		Pagination list = orderServiceService.selectPage(criteria, pagination);
		List<ServiceContent> servicelist=serviceContentService.selectAll();
		request.setAttribute("pagination", list);
		request.setAttribute("servicelist", servicelist);
		request.setAttribute("bean", bean);
		return "order/unarrorderlist";
	}
	
	/**
	 * 安排保姆页面
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/allarrangnanny", method = RequestMethod.GET)
	public String allarrangnanny(HttpServletRequest request, int oId) {
		OrderService orderService = orderServiceService.selectOne(oId);
		ServiceContent serviceContent=serviceContentService.selectOne(orderService.getServiceId());
		List<Nanny> nannylist=nannyService.selectAll();
		request.setAttribute("nannylist", nannylist);
		request.setAttribute("serviceContent", serviceContent);
		request.setAttribute("bean", orderService);
		return "order/allarrangnanny";
	}
	
	/**
	 * 安排保姆页面
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/toarrangnanny", method = RequestMethod.GET)
	public String toarrangnanny(HttpServletRequest request, int oId) {
		OrderService orderService = orderServiceService.selectOne(oId);
		ServiceContent serviceContent=serviceContentService.selectOne(orderService.getServiceId());
		List<Nanny> nannylist=nannyService.selectAll();
		request.setAttribute("nannylist", nannylist);
		request.setAttribute("serviceContent", serviceContent);
		request.setAttribute("bean", orderService);
		return "order/arrangnanny";
	}
	
	/**
	 * 安排保姆页面
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/tounorderarrangnanny", method = RequestMethod.GET)
	public String edit(HttpServletRequest request, int oId) {
		OrderService orderService = orderServiceService.selectOne(oId);
		ServiceContent serviceContent=serviceContentService.selectOne(orderService.getServiceId());
		List<Nanny> nannylist=nannyService.selectAll();
		request.setAttribute("nannylist", nannylist);
		request.setAttribute("serviceContent", serviceContent);
		request.setAttribute("bean", orderService);
		return "order/unorderarrangnanny";
	}
	
	/**
	 * 安排保姆
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/arrangenanny",method= RequestMethod.GET)
	@ResponseBody
    public JsonResult arrangeNanny(HttpServletRequest request,HttpServletResponse response,int oid,int nid) {
		JsonResult result=new JsonResult();
		try{
			OrderService orderService = orderServiceService.selectOne(oid);
			orderService.setArrangeNannyId(nid);
			orderService.setArrange(1);
			orderServiceService.update(orderService);
		}catch(Exception e){
			result.addErrorCode(ErrorCode.SYS_ERR);
		}
		return result;
	}
}
